package cn.kgc.mes.service.impl;

import java.util.ArrayList;
import java.util.List;
import cn.kgc.mes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.kgc.mes.mapper.MachineryTypeMapper;
import cn.kgc.mes.domain.MachineryType;
import cn.kgc.mes.service.IMachineryTypeService;

/**
 * 设备类型设置Service业务层处理
 * 
 * @author xxf
 * @date 2025-09-10
 */
@Service
public class MachineryTypeServiceImpl implements IMachineryTypeService 
{
    @Autowired
    private MachineryTypeMapper machineryTypeMapper;

    /**
     * 查询设备类型设置
     * 
     * @param machineryTypeId 设备类型设置主键
     * @return 设备类型设置
     */
    @Override
    public MachineryType selectMachineryTypeByMachineryTypeId(Long machineryTypeId)
    {
        return machineryTypeMapper.selectMachineryTypeByMachineryTypeId(machineryTypeId);
    }

    /**
     * 查询设备类型设置列表
     * 
     * @param machineryType 设备类型设置
     * @return 设备类型设置
     */
    @Override
    public List<MachineryType> selectMachineryTypeList(MachineryType machineryType)
    {
        return machineryTypeMapper.selectMachineryTypeList(machineryType);
    }

    /**
     * 新增设备类型设置
     * 
     * @param machineryType 设备类型设置
     * @return 结果
     */
    @Override
    public int insertMachineryType(MachineryType machineryType)
    {
        machineryType.setCreateTime(DateUtils.getNowDate());
        return machineryTypeMapper.insertMachineryType(machineryType);
    }

    /**
     * 修改设备类型设置
     * 
     * @param machineryType 设备类型设置
     * @return 结果
     */
    @Override
    public int updateMachineryType(MachineryType machineryType)
    {
        machineryType.setUpdateTime(DateUtils.getNowDate());
        return machineryTypeMapper.updateMachineryType(machineryType);
    }

    /**
     * 批量删除设备类型设置
     * 
     * @param machineryTypeIds 需要删除的设备类型设置主键
     * @return 结果
     */
    @Override
    public int deleteMachineryTypeByMachineryTypeIds(Long[] machineryTypeIds)
    {
        return machineryTypeMapper.deleteMachineryTypeByMachineryTypeIds(machineryTypeIds);
    }

    /**
     * 删除设备类型设置信息
     * 
     * @param machineryTypeId 设备类型设置主键
     * @return 结果
     */
    @Override
    public int deleteMachineryTypeByMachineryTypeId(Long machineryTypeId)
    {
        return machineryTypeMapper.deleteMachineryTypeByMachineryTypeId(machineryTypeId);
    }

    @Override
    public List<MachineryType> selectMachineryTypeTree(MachineryType machineryType) {
        List<MachineryType> list = machineryTypeMapper.selectMachineryTypeList(machineryType);
        return buildTree(list, 0L); // 0L 作为顶层父节点（根据你的库数据来）
    }
    private List<MachineryType> buildTree(List<MachineryType> list, Long parentId) {
        List<MachineryType> tree = new ArrayList<>();
        for (MachineryType node : list) {
            if (node.getParentTypeId().equals(parentId)) {
                node.setChildren(buildTree(list, node.getMachineryTypeId()));
                tree.add(node);
            }
        }
        return tree;
    }
}
